package cn.gjxblog.thread.example.aqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author gjx
 * @version v1.0
 * @Description
 * @date 2019/7/2 10:39
 */
@Slf4j
public class CountDownLatchExample1 {
    private final static  int threadCount = 200;

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(8);

        final CountDownLatch countDownLatch = new CountDownLatch(threadCount);
        long startTime = System.currentTimeMillis();

        for (int i = 0; i < threadCount; i++) {
            int finalI = i;
            executorService.execute(()->{
                test(finalI);
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        long endTime = System.currentTimeMillis();

        log.info("finish all cost {} time",endTime-startTime);
        executorService.shutdown();

    }

    public static void test(int finalI){
        int sum =0;
        for(int i = 0; i<100000000;i++){
            sum+=i;
        }
        log.info("thread :{}",finalI);
    }
}
